package com.core.excel;

import java.io.File;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;

import com.core.util.DspUtil;
import com.dsp.bean.InsureProgram;
import com.dsp.services.InsureProgramService;

import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class WriterExcel {
	int cols = 1;
	private WritableWorkbook writable ;
	public String running(String Code , String path ,List<Map<String,String>> data){
		String rtnPath = "";
		List<InsureProgram> itemList = null;
		try {
			rtnPath = System.currentTimeMillis()+".xls";
			if(!new File(path + File.separator+"downloads").exists())
				new File(path + File.separator+"downloads").mkdir();
			writable = JxlObject.createWorkBook(path + File.separator + "downloads" + File.separator + rtnPath);
			WritableSheet ws = JxlObject.createWritableSheet(0, writable,"社保结果");
			itemList = this.getItem();
			this.CreateTitle(ws , itemList);
			this.setValues(ws , itemList , data);
			writable.write();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		} finally {
			try {
				writable.close();
			} catch (WriteException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return rtnPath;
	}
	
	/**
	 * 写入值
	 * @param ws
	 * @param itemList
	 * @param data
	 * @throws WriteException 
	 */
	private void setValues(WritableSheet ws, List<InsureProgram> itemList , List<Map<String,String>> data) throws WriteException {
		Label label = null;
		
		for (Map<String,String> map : data){
			label = JxlObject.createLabel(0, cols, map.get("yearMonth"));
			ws.addCell(label);
			label = JxlObject.createLabel(1, cols, map.get("empname"));
			ws.addCell(label);
			label = JxlObject.createLabel(2, cols, map.get("hukou"));
			ws.addCell(label);
			label = JxlObject.createLabel(3, cols, map.get("company"));
			ws.addCell(label);
			int itemCols = 4;
			for(InsureProgram ip : itemList) {
				label = JxlObject.createLabel(itemCols++, cols, map.get("1_"+ip.getId()+"_Base"));
				ws.addCell(label);
				label = JxlObject.createLabel(itemCols++, cols, map.get("1_"+ip.getId()+"_Money"));
				ws.addCell(label);
			}
			for(InsureProgram ip : itemList){
				label = JxlObject.createLabel(itemCols++, cols, map.get("2_"+ip.getId()+"_Base"));
				ws.addCell(label);
				label = JxlObject.createLabel(itemCols++, cols, map.get("2_"+ip.getId()+"_Money"));
				ws.addCell(label);
			}
			cols++;
		}
	}
	
	/**
	 * 设置标题
	 * @param ws
	 * @throws WriteException 
	 */
	private void CreateTitle(WritableSheet ws , List<InsureProgram> itemList) throws WriteException{
		Label label = JxlObject.createLabel(0, 0, "计算年月");
		ws.addCell(label);
		label = JxlObject.createLabel(1, 0, "姓名");
		ws.addCell(label);
		label = JxlObject.createLabel(2,0,"户口性质");
		ws.addCell(label);
		label = JxlObject.createLabel(3,0, "公司");
		ws.addCell(label);
		int index=4;
		for(InsureProgram ip : itemList){
			label = JxlObject.createLabel(index++, 0, ip.getInsureName()+"基数(个人)");
			ws.addCell(label);
			label = JxlObject.createLabel(index++, 0, ip.getInsureName()+"(个人)");
			ws.addCell(label);
		}
		for(InsureProgram ip : itemList){
			label = JxlObject.createLabel(index++, 0, ip.getInsureName()+"基数(公司)");
			ws.addCell(label);
			label = JxlObject.createLabel(index++, 0, ip.getInsureName()+"(公司)");
			ws.addCell(label);
		}
	}
	
	/**
	 * 社保项目
	 * @return
	 */
	private List<InsureProgram> getItem(){
		InsureProgramService ips = (InsureProgramService)DspUtil.getBean("insureProgramService");
		DetachedCriteria dc = DetachedCriteria.forClass(InsureProgram.class);
		List<InsureProgram> list = ips.likeBy(dc);
		return list;
	}
}
